<template>
  <div class="box">
    <el-card>
      <el-form ref="loginForm" :model="form" :rules="rules" class="login-box">
        <h2 class="login-title">注册</h2>
        <el-form-item prop="username">
          <el-input type="text" placeholder="请输入账号" v-model="form.username"/>
        </el-form-item>

        <el-form-item prop="name">
          <el-input type="text" placeholder="请输入姓名" v-model="form.name"/>
        </el-form-item>
        <el-form-item prop="sex" label="请选择性别">
          <template>
            <el-radio v-model="form.sex" label="男">男</el-radio>
            <el-radio v-model="form.sex" label="女">女</el-radio>
          </template>
        </el-form-item>

        <el-form-item prop="password">
          <el-input type="password" placeholder="请输入密码" v-model="form.password"/>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="text" placeholder="请再次输入密码" v-model="form.phone"/>
        </el-form-item>
        <el-form-item prop="access">
          <template>
            <el-radio v-model="form.access" label="0">普通用户注册</el-radio>
            <el-radio v-model="form.access" label="1">医生注册</el-radio>
            <el-radio v-model="form.access" label="2">管理员注册</el-radio>
          </template>
        </el-form-item>
        <el-form-item>
          <div class="login" @click="register('loginForm')">提交</div>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'register',

  data() {
    return {
      access: '0',
      imageUrl: 'http://localhost:8085/dept/createImageCode',
      form: {
        username: '',
        password: '',
        phone: '',
        name: '',
        access:'0',
        sex: '男'
      },
      //表单验证，需要在 el-form-item 元素中增加prop属性
      rules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
          {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
        ],

        phone: [
          {required: true, message: '请输入电话', trigger: 'blur'},
          {pattern: /^\d{11}$/, message: '请输入11位数字的电话号码', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'}
        ],
      },
    };
  },

  mounted() {

  },

  methods: {
    // 获取验证码图片
    uploadImage() {
      // this.imageUrl = getVerifyImage().res.data
      this.imageUrl = ''
      this.$nextTick(() => {
        this.imageUrl = "http://localhost:8085/dept/createImageCode"
      })
    },
    //登录
    register(formName) {
      console.log("1111111")
      //将数据进行预校验
      this.$refs[formName].validate(async valid => {
        if (!valid) return
        //这里就发起axios请求后台数据判断是否存在该用户
        const {data: res} = await this.$http.post('/user/register', this.form)
        // console.log(res)
        //管理员存在
        if (res.status) {
          //将登录成功的 管理员返回 存储下了
          window.sessionStorage.setItem('user', JSON.stringify(res.data))
          console.log(res.data)
          //登录成功跳转到主页
          this.$message.success('注册成功')
          this.$router.push('/login')
        } else {
          this.$message.error(res.msg)
        }
      })
    },

  },
};
</script>

<style lang='scss' scoped>
.box {
  width: 100%;
  height: 100vh;
  background-image: url("../assets/images/medical-background.png");
  background-repeat: no-repeat;
  background-size: cover;

  .el-card {
    width: 26%;
    transform: translateY(50%);
    margin-left: 37%;
    border-radius: 20px;
    .login-box {
      text-align: center;
      min-height: 200px;
      padding: 10px;
      .login-title {
        color: #9c9999;
        transform: translateY(-20px);
      }

      .login {
        color: white;
        width: 100%;
        background-color: #6ECAE1;
        border-radius: 4px;
      }
    }
  }
}
</style>
